package com.toolset.Adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.toolset.Bean.ToolItem;
import com.toolset.R;

import java.util.List;

/**
 * 主页信息布局界面适配器
 */
//继承内部类
public class ToolItemAdapter extends RecyclerView.Adapter<ToolItemAdapter.ViewHolder>{

    //数据
    private List<ToolItem> list;



    //构造函数
    public ToolItemAdapter(List<ToolItem> list) {
        this.list = list;
    }


    //创建视图并初始化
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //绑定模板
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tool_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //绑定事件
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final ToolItem toolItem = list.get(position);

        holder.iconView.setImageResource(toolItem.getToolItemIcon());
        holder.nameView.setText(toolItem.getToolItemName());

        //设置点击事件
        holder.iconView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //    Toast.makeText(v.getContext(),"跳转："+ toolItem.getToolItemName(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(v.getContext(),toolItem.getIntendUrl());
                v.getContext().startActivity(intent);
            }
        });

        holder.nameView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //   Toast.makeText(v.getContext(),"跳转："+ toolItem.getToolItemName(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(v.getContext(),toolItem.getIntendUrl());
                v.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //内部类
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView iconView;
        TextView nameView;

        //内部类构造函数
        public ViewHolder(View view) {
            super(view);
            iconView = (ImageView) view.findViewById(R.id.tool_item_icon);
            nameView = (TextView) view.findViewById(R.id.tool_item_name);


        }
    }
}
